package com.xb.dao;

import com.xb.entity.Meeting;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;

public class MeetingDao extends BaseDao{


    /**
     * 会议搜索+分页
     * @param title
     * @param status
     * @param startIndex
     * @param pageSize
     * @return
     */
    public List<Meeting> findPage(String title, String status, Integer startIndex, Integer pageSize) {
        String sql = "";
        Object[] args = {};

        if (status.equals("-1")) {
            // 说明前端没有传递status参数
            sql = "select * from meeting where title like ? order by publish_date desc limit ?,?";
            args = new Object[]{"%" + title + "%", startIndex, pageSize};
        } else {
            sql = "select * from meeting where title like ? and status =? order by publish_date desc limit ?,?";
            args = new Object[]{"%" + title + "%", status, startIndex, pageSize};
        }

        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Meeting.class), args);
    }


    /**
     *
     * @param title
     * @param status
     * @return
     */
    public Integer findPageCount(String title, String status) {
        String sql = "";
        Object[] args = {};

        if (status.equals("-1")) {
            // 说明前端没有传递status参数
            sql = "select count(*) from meeting where title like ?";
            args = new Object[]{"%" + title + "%"};
        } else {
            sql = "select count(*) from meeting where title like ? and status = ? ";
            args = new Object[]{"%" + title + "%", status};
        }

        return jdbcTemplate.queryForObject(sql, Integer.class, args);
    }


    /**
     * 保存会议
     * @param meeting
     */
    public void save(Meeting meeting) {
        jdbcTemplate.update("insert into meeting values(null,?,?,?,?,?,?,?,?,?)",
                meeting.getDeptName(),
                meeting.getDeptId(),
                meeting.getTitle(),
                meeting.getContent(),
                meeting.getPublishDate(),
                meeting.getStartTime(),
                meeting.getEndTime(),
                meeting.getStatus(),
                meeting.getMakeUser()
        );
    }


    /**
     * 会议详情
     * @param meetingId
     * @return
     */
    public Meeting findById(Long meetingId) {
        String sql="select * from meeting where id=?";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(Meeting.class),meetingId);
    }


    /**
     * 会议实到人数
     * @param meetingId
     * @return
     */
    public Integer findRealCount(Long meetingId) {
        String sql="select count(*) from meeting_join where m_id=?";
        return jdbcTemplate.queryForObject(sql, Integer.class,meetingId);

    }


    /**
     * 我是否已经参加了本次会议
     * @param id
     * @param meetingId
     * @return
     */
    public int isJoinMeeting(Long id, Long meetingId) {
        String sql="select count(*) from meeting_join where m_id=? and u_id=?";
        return jdbcTemplate.queryForObject(sql, Integer.class,meetingId,id);
    }


    /**
     * 取消会议
     * @param id
     * @param meetingId
     */
    public void delJoinMeeting(Long id, Long meetingId) {
        jdbcTemplate.update("delete from meeting_join where u_id=? and m_id=?",id,meetingId);
    }


    /**
     * 参加会议
     * @param id
     * @param meetingId
     */
    public void joinMeeting(Long id, Long meetingId) {
        jdbcTemplate.update("insert into meeting_join values (?,?)",id,meetingId);
    }
}
